﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using CSequence;
using Ini;
using System.IO.Pipes;
using System.Data.OleDb;

namespace Sequence
{
    public partial class FrmProperties : Form
    {
        public OleDbConnection m_connection;
        
        public FrmProperties()
        {
            InitializeComponent();
        }
        public CStep m_oStep = null;
        public bool bToSave = false;

        public void LoadData(CStep oStep)
        {
            Text = "Properties (" + oStep.sProcessId + "." + oStep.sId + ")"; 
            
            m_oStep = oStep;
            tbName.Text = oStep.sName;
            cbType.Text = oStep.sType;
            tbQueryDef.Text = oStep.sQueryName;
            cbMode.Text = oStep.sMode;
            

            foreach (CInput oIn in oStep.lInputs)
            {
                int iRow = dgvInput.Rows.Add();
                dgvInput.Rows[iRow].Cells[0].Value = oIn.sName;
                dgvInput.Rows[iRow].Cells[1].Value = oIn.sValue;
            }

            foreach (COutput oOut in oStep.lOutputs)
            {
                int iRow = dgvOutput.Rows.Add();
                dgvOutput.Rows[iRow].Cells[0].Value = oOut.sName;
                dgvOutput.Rows[iRow].Cells[1].Value = oOut.sValue;
            }

            foreach (CParam oPar in oStep.lParams)
            {
                int iRow = dgvParams.Rows.Add();
                dgvParams.Rows[iRow].Cells[0].Value = oPar.sName;
                dgvParams.Rows[iRow].Cells[1].Value = oPar.sValue;
            }
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            
            m_oStep.sName = tbName.Text;
            m_oStep.sType =  cbType.Text;
            m_oStep.sQueryName =  tbQueryDef.Text;
            m_oStep.sMode = cbMode.Text;
            
            m_oStep.lInputs.Clear();
            int i = 1; 
            foreach (DataGridViewRow oRow in dgvInput.Rows)
            {
                if ((oRow.Cells[0].Value != null) && (oRow.Cells[1].Value != null))
                {
                    CInput oInput = new CInput(oRow.Cells[0].Value.ToString(), oRow.Cells[1].Value.ToString(),i.ToString(), m_oStep);
                    i++;
                    m_oStep.lInputs.Add(oInput);
                }
            }


            m_oStep.lOutputs.Clear();
            foreach (DataGridViewRow oRow in dgvOutput.Rows)
            {
                if ((oRow.Cells[0].Value != null) && (oRow.Cells[1].Value != null))
                {
                    COutput oOutput = new COutput(oRow.Cells[0].Value.ToString(), oRow.Cells[1].Value.ToString(), i.ToString(), m_oStep);
                    i++;
                    m_oStep.lOutputs.Add(oOutput);
                }
            }

            m_oStep.lParams.Clear();
            foreach (DataGridViewRow oRow in dgvParams.Rows)
            {
                if ((oRow.Cells[0].Value != null) && (oRow.Cells[1].Value != null))
                {
                    CParam oPar = new CParam(oRow.Cells[0].Value.ToString(), oRow.Cells[1].Value.ToString(), i.ToString(), m_oStep);
                    i++;
                    m_oStep.lParams.Add(oPar);
                }
            }
            bToSave = true;
            this.Close();
        }

        private void FrmProperties_Load(object sender, EventArgs e)
        {

        }

        private void cmdGetQueydef_Click(object sender, EventArgs e)
        {
            FrmQueryDef oFrmQueryDef = new FrmQueryDef(cbType.Text);
            oFrmQueryDef.m_connection = m_connection;

            oFrmQueryDef.ShowDialog();
            if ( oFrmQueryDef.sID != null)
                if (oFrmQueryDef.sID.Length > 0)
                    tbQueryDef.Text = oFrmQueryDef.sID;
        }

        private void dgvInput_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells.  
            if (e.RowIndex < 0 || e.ColumnIndex == dgvInput.Columns["cEdit"].Index)
            {

                String sSql = "select 1,t_api_qd_process_step.name || '>' || t_api_qd_process_step_params.name as value,t_api_qd_process_step_params.name ";
                sSql += "from t_api_qd_process_step_params join t_api_qd_process_step on t_api_qd_process_step_params.process_id = t_api_qd_process_step.process_id ";
                sSql += "and t_api_qd_process_step_params.step_id = t_api_qd_process_step.id where t_api_qd_process_step_params.params_type='O' and t_api_qd_process_step_params.process_id=" + m_oStep.sProcessId + " ";
                sSql += "and t_api_qd_process_step_params.step_id<" + m_oStep.sId + " ";
                sSql += "union ";
                sSql += "select 100,'?' as value,t_api_querydef_fields.alias as name from t_api_querydef_fields join t_api_querydef  on t_api_querydef.id = t_api_querydef_fields.def_id ";
                sSql += "where t_api_querydef.query_name = '" + tbQueryDef.Text + "' ";
                sSql += "order by 1,2";

                FrmGetParam oFrmGetParam = new FrmGetParam(m_connection, sSql);
                oFrmGetParam.ShowDialog();
                if ((dgvInput.Rows[e.RowIndex].Cells[0].Value == "") || (dgvInput.Rows[e.RowIndex].Cells[0].Value == null))
                    dgvInput.Rows[e.RowIndex].Cells[0].Value = oFrmGetParam.sName;
                dgvInput.Rows[e.RowIndex].Cells[1].Value = oFrmGetParam.sValue;

            }
        }

        private void dgvOutput_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells.  
            if (e.RowIndex < 0 || e.ColumnIndex == dgvOutput.Columns["cOutEdit"].Index)
            {
                String sSql = "";

                if (cbType.Text == "QueryDef")
                {
                    sSql = "  select 'o' || t_api_querydef_fields.alias as NAME ";
                    sSql += "    , '/Root/XMLBody/Queries/' || t_api_querydef.query_name || '/' || t_api_querydef.alias ||'/' || t_api_querydef_fields.alias ||'/text()' as VALUE  ";
                    sSql += "    from t_api_qd_process_step join t_api_querydef on t_api_querydef.query_name=t_api_qd_process_step.query_name and t_api_querydef.dbms ='N/A' ";
                    sSql += "                           join t_api_querydef_fields on t_api_querydef.id = t_api_querydef_fields.def_id ";
                    sSql += "    where t_api_qd_process_step.id=" + m_oStep.sId + " and t_api_qd_process_step.process_id=" + m_oStep.sProcessId + " ";
                    sSql += "union  ";
                    sSql += "    select 'Todo' as name, '?' as VALUE from dual ";
                    sSql += "union  ";
                    sSql += "    select 'i' || t_api_qd_process_step_params.name, 'Input>' || t_api_qd_process_step_params.name ";
                    sSql += "    from t_api_qd_process_step_params ";
                    sSql += "    where t_api_qd_process_step_params.step_id=" + m_oStep.sId + " and t_api_qd_process_step_params.process_id=" + m_oStep.sProcessId + " and params_type='I' ";
                    sSql += "order by 1,2 ";

                }
                else
                {

                    sSql = "  select 'o' || taqf2.alias as NAME ";
                    sSql += "    , '/Root/XMLBody/Queries/' || taq2.query_name || '/' || taq2.alias ||'/' || taqf2.alias ||'/text()' as VALUE  ";
                    sSql += "    from t_api_qd_process_step join t_api_querydef on t_api_querydef.query_name=t_api_qd_process_step.query_name and t_api_querydef.dbms ='N/A' ";
                    sSql += "                           join t_api_querydef_fields on t_api_querydef.id = t_api_querydef_fields.def_id ";
                    sSql += "                           join t_api_querydef taq2 on t_api_querydef.return_querydef = taq2.query_name ";
                    sSql += "    join t_api_querydef_fields taqf2 on t_api_querydef.id = taqf2.def_id ";
                    sSql += "    where t_api_qd_process_step.id=" + m_oStep.sId + " and t_api_qd_process_step.process_id=" + m_oStep.sProcessId + " ";
                    sSql += "union  ";
                    sSql += "    select 'Todo' as name, '?' as VALUE from dual ";
                    sSql += "union  ";
                    sSql += "    select 'i' || t_api_qd_process_step_params.name, 'Input>' || t_api_qd_process_step_params.name ";
                    sSql += "    from t_api_qd_process_step_params ";
                    sSql += "    where t_api_qd_process_step_params.step_id=" + m_oStep.sId + " and t_api_qd_process_step_params.process_id=" + m_oStep.sProcessId + " and params_type='I' ";
                    sSql += "order by 1,2 ";

                }

                FrmGetParam oFrmGetParam = new FrmGetParam(m_connection, sSql);
                oFrmGetParam.ShowDialog();
                if ((oFrmGetParam.sName + oFrmGetParam.sValue).Length > 0)
                {
                    dgvOutput.Rows[e.RowIndex].Cells[0].Value = oFrmGetParam.sName;
                    dgvOutput.Rows[e.RowIndex].Cells[1].Value = oFrmGetParam.sValue;
                }
            }
        }

        private void dgvParams_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells.  
            if (e.RowIndex < 0 || e.ColumnIndex == dgvParams.Columns["cParEdit"].Index)
            {
                // Hello
                int i;
                i = 10;
            }
        }

        private void dgvInput_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
